package com.zero.youxiao.service;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zero.basic.exception.SmsErrorConstant;
import com.zero.basic.exception.SmsRuntimeException;
import com.zero.basic.exception.SmsStatusCode;
import com.zero.youxiao.dao.MenuDao;
import com.zero.youxiao.dto.MenuDto;

@Service("indexService")
@Transactional(rollbackFor = Exception.class)
public class IndexService {

	@Inject
	private MenuDao menuDao;

	@Transactional()
	public List<MenuDto> getAllMenus() throws SmsRuntimeException {
		try {
			List<MenuDto> allMenuDtos = menuDao.getMenuTree();
			
			System.out.println(allMenuDtos);
			List<MenuDto> menuDtos = new ArrayList<MenuDto>();
			for(MenuDto userMenuDto : allMenuDtos){
				List<MenuDto> subMenuDtos = new ArrayList<MenuDto>();
				boolean haveSub = false;
				for (MenuDto tempMenu : allMenuDtos) {
					if(tempMenu.getParent()!=null){
						if(userMenuDto.getId() == tempMenu.getParent()){
							haveSub=true;
							subMenuDtos.add(tempMenu);
						}
					}
				}
				if(haveSub){
					System.out.println("dsa");
					userMenuDto.setSubMenus(subMenuDtos);
					menuDtos.add(userMenuDto);
				}
			}
			System.out.println(menuDtos);
			
			return menuDtos;
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,
					SmsErrorConstant.WEB_APPLICATION, "服务器异常");
		}
	}

}
